package com.hmdp.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.entity.Shop;
import com.hmdp.service.IShopService;
import com.hmdp.utils.constant.SystemConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Controller 商铺信息
 */
@Api(tags = "商铺信息")
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    public IShopService shopService;

    @ApiOperation("根据商铺id查询")
    @ApiImplicitParam(name = "id", value = "商铺id", required = true, example = "1", dataType = "long")
    @GetMapping("/{id}")
    public Result queryShopById(@PathVariable("id") Long id) {
        return shopService.queryById(id);
    }

    @ApiOperation("新增商铺")
    @PostMapping
    public Result saveShop(@RequestBody Shop shop) {
        // 写入数据库
        shopService.save(shop);
        // 返回店铺id
        return Result.ok(shop.getId());
    }

    @ApiOperation("根据商铺id更新")
    @PutMapping
    public Result updateShop(@RequestBody Shop shop) {
        // 写入数据库
        return shopService.update(shop);
    }

    @ApiOperation("根据商铺类型分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "类型id", required = true, example = "1", dataType = "int"),
            @ApiImplicitParam(name = "current", value = "查询页", required = true, example = "1", dataType = "int"),
            @ApiImplicitParam(name = "x", value = "经度", required = false, example = "120.149192", dataType = "double"),
            @ApiImplicitParam(name = "y", value = "维度", required = false, example = "30.316078", dataType = "double")})
    @GetMapping("/of/type")
    public Result queryShopByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "x", required = false) Double x,
            @RequestParam(value = "y", required = false) Double y) {
        return shopService.queryShopByType(typeId, current, x, y);
    }

    @ApiOperation("根据商铺名称关键字分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "商铺名称关键字", required = false, example = "", dataType = "string"),
            @ApiImplicitParam(name = "current", value = "查询页", required = true, example = "1", dataType = "int")})
    @GetMapping("/of/name")
    public Result queryShopByName(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 根据类型分页查询
        Page<Shop> page = shopService.query()
                .like(StrUtil.isNotBlank(name), "name", name)
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 返回数据
        return Result.ok(page.getRecords());
    }

}
